﻿using Bear.Library.Util;
using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Bear.Models
{
    [Serializable]
    public class Map
    {
        public Map()
        {
            this.Steps = new List<Step>();
        }

        public string Name { get; set; }

        public int Left { get; set; }
        public int Top { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }

        [NonSerialized]
        private Bitmap _Image;


        public Bitmap Image
        {
            get { return _Image; }
            set { _Image = value; }
        }

        public string MonsterMarkName { get; set; }


        [NonSerialized]
        private Image<Gray, byte> _MonsterMark = null;

        public Image<Gray, byte> MonsterMark
        {
            set
            {
                this._MonsterMark = value;
            }
            get
            {
                return _MonsterMark;
            }
        }

        public List<Step> Steps { set; get; }

        public void Save()
        {
            string filePath = Path.Combine(Maps.DIRECTORY_PATH, this.Name + ".xml");
            SerializerHelper.XmlSerializer(this, filePath);
        }

        public string DisplayString
        {
            get
            {
                return string.Format("{0}:{1}x{2}", Name, Width, Height);
            }
        }
    }

    [Serializable]
    public class Maps : List<Map>
    {
        public const string DIRECTORY_PATH = @"Datas\Maps";

        public static Maps Load()
        {
            Maps maps = new Maps();
            if (!Directory.Exists(DIRECTORY_PATH))
            {
                Directory.CreateDirectory(DIRECTORY_PATH);
            }
            foreach (var item in Directory.GetFiles(DIRECTORY_PATH, "*.xml"))
            {
                Map map = SerializerHelper.XmlDeserialize<Map>(item);
                maps.Add(map);
            }

            return maps;

        }

        public void Save()
        {

            if (!Directory.Exists(DIRECTORY_PATH))
            {
                Directory.CreateDirectory(DIRECTORY_PATH);
            }

            foreach (var item in Directory.GetFiles(DIRECTORY_PATH))
            {
                File.Delete(item);
            }

            foreach (var item in this)
            {
                item.Save();
            }
        }

    }

    [Serializable]
    public class Step
    {
        public int ID { get; set; }
        public Point Left { get; set; }
        public Point Right { get; set; }
        public Point ConnectionPoint { get; set; }
        public Directions ConnectionPointMode { get; set; }
        public bool IsJump { get; set; }
        public int Duration { get; set; }
        public string DisplayString
        {
            get
            {
                return string.Format("左:{0}\r\n右:{1}\r\n连:{2} {3}\r\n跳:{4}\r\n时间:{5}",
                    this.Left, this.Right, this.ConnectionPoint, this.ConnectionPointMode, this.IsJump ? "是" : "否", this.Duration);
            }
        }
    }

}
